﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrNKMayBao : DevComponents.DotNetBar.Office2007Form
    {
        public FrNKMayBao()
        {
            InitializeComponent();
        }
        private int i = 0, j = 0;
        private string temp,NgayStart1,NgayEnd1;
        private void comboload()
        {
            string[] arr_MayBao = new string[] { "Máy 1", "Máy 2", "Máy 3", "Máy 4", "Máy 5", "Máy 6", "Máy 7", "Máy 8", "Máy 9" };

            for (j = 0; j < arr_MayBao.Length; j++)
            {
               
                dataGridViewX1.Rows.Add(false, arr_MayBao[j].ToString(), 0,"");
            }
            maskedTextBox3.Text = DateTime.Now.ToString("HH:mm");
            Process prs = new Process();
            prs.formatcolordatagrid(dataGridViewX1);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboload();
        }
        private void save()
        {
            string inserts = null;
            Process prs = new Process();
            SqlConnection Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;

            string[] temp2 = dateTimePicker1.Text.Split('/');
            temp = temp2[1] + "/" + temp2[0] + "/" + temp2[2] + " " + maskedTextBox3.Text;
            MessageBox.Show(temp);
            try
            {
                for (i = dataGridViewX1.Rows.Count - 2; i >= 0; i--)
                {
                   
                    //DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridViewX1.Rows[i].Cells["Checkbox"];
                    inserts = @"INSERT INTO [dbo].[KV1_NKMayBao] ([TenMay],[Mtoi],[GhiChu],[Ngay])VALUES(N'" + dataGridViewX1.Rows[i].Cells["MayBao"].Value.ToString() + "'," + dataGridViewX1.Rows[i].Cells["MToi"].Value.ToString() + ",N'" + dataGridViewX1.Rows[i].Cells["GhiChu"].Value.ToString()+"','"+temp+"')" ;
                   

                    cmd.CommandText = inserts;
                    cmd.ExecuteNonQuery();
                    dataGridViewX1.Rows.RemoveAt(i);
                }
                MessageBox.Show("Dữ liệu được lưu thành công");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Dữ liệu lưu không thành công",ex.Message);
                MessageBox.Show(inserts);
            }
        }

        private void bt_saveinput_Click(object sender, EventArgs e)
        {
            save();
        }

      

        private void check_all_CheckedChanged_1(object sender, EventArgs e)
        {
            if (check_all.Checked == true)
            {

                for (i = 0; i < dataGridViewX1.Rows.Count - 1; i++)
                {

                    DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridViewX1.Rows[i].Cells[0];
                    chk.Value = chk.TrueValue;

                }
            }
            else
            {

                for (i = 0; i < dataGridViewX1.Rows.Count - 1; i++)
                {
                    DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridViewX1.Rows[i].Cells[0];
                    chk.Value = chk.FalseValue;
                }
            }
        }
        private void search()
        {
            Process prs = new Process();

            string[] temp2 = txt_DateStart.Text.Split('/');
            NgayStart1 = temp2[1] + "/" + temp2[0] + "/" + temp2[2] + " " + maskedTextBox1.Text;
            string[] temp1 = txt_DateStart.Text.Split('/');
            NgayEnd1 = temp1[1] + "/" + temp1[0] + "/" + temp1[2]+ " " + maskedTextBox2.Text;
            try
            {
                if (check_search.Checked == true)
                {
                    temp = @"SELECT  [TenMay],sum([Mtoi]) as MToi,sum(MToi)/(datediff(hh,'" + NgayStart1 + "','" + NgayEnd1 + "')) as MtoiH,550 as ChiTieuH,left(sum(MToi)*100/(datediff(hh,'" + NgayStart1 + "','" + NgayEnd1 + "')*550),5)+' %' as TyLe";
                    temp += @"    FROM [dbo].[KV1_NKMayBao] where Ngay>='" + NgayStart1 + "' and Ngay<='" + NgayEnd1 + "' group by [TenMay] ";
                }
                if (check_search.Checked == false)
                {
                    temp = @"SELECT 
                      [TenMay]
                      ,[Mtoi]
                      ,[GhiChu]
                      ,[Ngay]
                  FROM [dbo].[KV1_NKMayBao] where Ngay>='" + NgayStart1 + "' and Ngay<='" + NgayEnd1 + "' order by [TenMay]";
                }

                DataSet ds = prs.dts(temp);
                dataGridViewX2.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void bt_search_Click(object sender, EventArgs e)
        {
            search();
        }
    }
}
